<template>
  <div class="autoComplete">
    <div class="autoCompleteInput">
      <input 
        type="text" 
        class="input" 
        v-model="selected" 
        @change="getInput"
        placeholder="请输入查询内容">
    </div>
    <div class="autoCompleteTips">
      <div 
        class="autoCompleteTipsList"
        v-for="(item, index) in list" 
        :key="index"
        @click="select(item)">{{item}}</div>
        <slot></slot>
    </div>
  </div>
</template>

<script>
export default {
  name: 'AutoComplete',
  props: {
    list: {
      type: Array,
      default: () => []
    }
  },
  data () {
    return {
      selected: ''
    }
  },
  methods: {
    select (item) {
      this.selected = item
      this.$emit('getValue', item)
    },
    getInput () {
      this.$emit('select', this.selected)
    }
  },
  watch: {
    selected: function () {
      this.getInput()
    }
  }
}
</script>

<style scoped>
    .autoCompleteInput {
        width: 100%;
        border: 1px solid #dddddd;
        border-radius: 4px;
        height: 38px;
        line-height: 38px;
    }
    .autoCompleteInput input{
        width: 95%;
        margin: 0 auto;
        height: 30px;
        display: inline-block;
        vertical-align: middle;
        border: none;
    }
    .autoCompleteInput input:focus,
    .autoCompleteInput input:active {
        outline: none;
    }
    .autoCompleteTips {
        padding: 0 10px;
    }
    .autoCompleteTips .autoCompleteTipsList{
        display: flex;
        margin: 6px 0;
    }
</style>
